var ns, app, dialog, toast;
app = new Vue({
    el: '#app',
    data: {
        list: [],
        form: {
            ID:0,
            pageindex: 0,
            pagesize: 10,
            servicetype: 1, //0-未选择 1-任务工单保修 2-投诉建议
            status: 4, //1-全部 0-处理中 1-已完工 4-待受理 5-待派单 10-待接单 50-已归档=已关单
            RoomID: 0,
            isviewdetail: true,
            onlybaoshi: 0,
            IsWorkOrder:1,
            isFirstLoad: 1
        },
        
        current_item_length: 0,
        is_searching: false,
        scroll_top: 0,
        can_scroll: false,
        showsearchmonth: false,
        showsearchparam: false,
        operation: {
            canVisit: false,
            canClose: false,
            canSend: false,
            canAccept: false,
            canAfreshSend: false,
            canChuidan: false,
            canRefuse: false,
            canAcceptOrder: false,
            canComplete: false,
            canHandle: false
        },
        showsearch:false,
        showTop:false,
        searchlist:[],
        projectList:[
            {"ID":"4","Name":"待受理"},
            {"ID":"5","Name":"待派单"},
            {"ID":"10","Name":"待接单"},
            {"ID":"0","Name":"处理中"},
        ],
        number:0,
        checked:false,
        Read:false,
        servicetypename:'',
        servicetypeid:'',
        chooseId:0,//选择的工单id
        showWin:false,
        isShowRead:false,
        touchID:0,//长按选中id
    },
    methods: {
        get_data: function () {
            var that = this;
            that.is_searching = true;
            that.can_scroll = false;
            var options = {};
            options.pagesize = that.form.pagesize;
            options.pageindex = that.form.pageindex;
            options.IsWorkOrder = that.form.IsWorkOrder;
            options.IsReadonly=that.Read;
            // if (that.form.pageindex >= 10){
            //     toast.loading({
            //         title: "提交中",
            //         duration: 2000
            //     }, function(ret) {});
            // }
            options.action = "getjpushlogservicelist";
            options.onlybaoshi = that.form.onlybaoshi;
            options.isAnalysis = that.form.isviewdetail ? 1 : 0;
            options.isFirstLoad = that.form.isFirstLoad;
            ns.post(options, function (succeed, data, err) {
                that.is_searching = false;
                if (succeed) {

                    // if (data.list.length == that.form.pagesize) {
                    //     that.can_scroll = true;
                    // }
                    // if (that.form.pageindex == 0) {
                    //     that.list =[];
                    //     setTimeout(() => {
                    //         window.scrollTo(0,document.body.scrollHeight);
                    //     }, 300);
                    // }
                    // data.list.forEach(function(item){
                    //     that.list.splice(0,0,item);
                    // })
                    // if(that.form.pageindex >= 10){
                    //     setTimeout(() => {
                    //         that.do_scrollbtn(data.list[0].JpushLogID);
                    //         toast.hide();
                    //     }, 100);
                    // }

                    if (data.list.length == that.form.pagesize) {
                        that.can_scroll = true;
                    }
                    if (that.form.pageindex == 0) {
                        that.list = data.list;
                    } else {
                        that.list = that.list.concat(data.list);
                    }

                    that.current_item_length = that.list.length;

                    that.form.isFirstLoad = 0;
                    that.number=data.number;
                    for(var i=0;i<data.list.length;i++){
                        data.list[i].JpushAddTime=ns.SetTime_style(data.list[i].JpushAddTime);
                    }
                    if(that.form.ID){
                        setTimeout(function() {
                            that.do_scrollTo();
                        }, 1000)
                    }
                    api.sendEvent({
                        name:'isreadCustomer'
                    })
                } else if (err) {
                    that.current_item_length = 0;
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        choose_noread:function(){
            var that=this;
            that.Read=!that.Read;
            that.get_data();
            // that.checked=!that.checked;
        },
        do_Read:function(){
            var that = this;
            var options={};
            options.action="jpushlogusersetread";
            options.JpushLogID=that.touchID;
            ns.post(options, function (succeed, data, err) {
                if(succeed){
                    that.showWin=false;
                    that.get_data();
                }
            });
        },
        //删除消息
        deleteItem:function(){
            var that = this;
            var options={};
            options.action="doremovepushmsg";
            options.ID=that.touchID;
            ns.post(options, function (succeed, data, err) {
                if(succeed){
                    that.showWin=false;
                    that.get_data();
                }
            });
        },
        All_read:function(){
            var that=this;
            var options={};
            options.action='servicemessageread';
            options.servicetype=that.form.servicetype;
            ns.post(options, function (succeed, data, err){
                if(succeed){
                    if(data.status){
                        api.toast({
                            msg: data.Msg,
                            duration: 2000,
                            location: 'bottom'
                        });
                        that.get_data();
                    }

                }else{
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            })
        },

        //点击遮罩关闭弹窗
        doHideWin:function(){
            var that=this;
            that.showWin=false;
            that.touchID=0;
            that.get_data();
        },
        //长按弹出操作框
        gettouchStart:function(index,JpushLogID,IsRead){
            var that = this;
            api.addEventListener({
                name:'longpress'
            }, function(ret){  
                that.showWin=!that.showWin; 
                if(!IsRead){
                    that.isShowRead=true;
                }else{
                    that.isShowRead=false;
                }
                that.touchID=JpushLogID;
            })
        },

        //翻页第一条数据的位置
        do_scrollbtn:function(id){
    　　　　 var sollid = document.getElementById('scroll'+id);
    　　　　 window.scrollTo(0,sollid.offsetTop); 
    　　},

        get_operation: function () {
            var that = this;
            var options = {};
            options.action = "getserviceoperation";
            options.servicetype = that.form.servicetype;
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    that.operation = data.operation;
                } else {
                }
            });
        },
        select_status: function (status) {
            var that = this;
            that.showTop=false;
            that.form.status = status;
            that.list = [];
            that.get_data();
        },
        openGongdan: function (item) {
            var that = this;
            var options={};
            options.action="jpushlogusersetread";
            options.JpushLogID=item.JpushLogID;
            ns.post(options, function (succeed, data, err) {
                if(succeed){
                    that.get_data();
                }
            });
            ns.openWin('home_task_con_frm', '任务', {
                id: item.ID,
                isviewdetail: that.form.isviewdetail
            });
        },
        doOpenServiceProcess: function (type) {
            ns.openWin('analysis_customerservice_process_frm', '工单进度');
        },
        open_renwu:function(){
            ns.openWin('customerservice_renwulist', '客服工单待办');
        },

        doHideSearch: function () {
            var that = this;
            that.showsearchparam = false;
            that.showsearchmonth = false;
            that.showsearch = false;
        },
        //判断是否受理
        do_isShouli:function(item){
            var that=this;
            that.chooseId=item.ID;
            if(!item.ServiceTypeIsDeep){
                ns.openWin('neibubaoshi_typelist', '工单类型选择',{
                    placetype:1,//1-微信公众号改工单类型
                    gongdanType:that.form.servicetype
                });
            }else{
                that.do_open_shouli();
            }
        },
        do_editType(){
            var that = this;
            var options={};
            options.ID=that.chooseId;
            options.TypeID=that.servicetypeid;
            options.TypeName=that.servicetypename;
            options.action="editcustomerservice";
            ns.post(options, function (succeed, data, err) {
                if(succeed){
                    that.do_open_shouli();
                }else{
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            })

        },
        // 受理
        do_open_shouli: function () {
            var that = this;
            var options = {};
            options.ID = that.chooseId;
            options.action = "customerserviceshouli";
            ns.post(options, function (succeed, data, err) {
                if (data.status == true) {
                    that.form.isFirstLoad = 1;
                    that.select_status(5);
                    api.sendEvent({
                        name: 'reloadservicelist',
                    });
                } else {
                    api.toast({
                        msg: data.Msg,
                        duration: 2000,
                        location: 'bottom'
                    });
                    that.form.isFirstLoad = 1;
                    that.get_data();
                }
            }, {
                toast: true,
                toastmsg: '提交中'
            });
        },
        // 派单
        do_open_chooseuser: function (item, type) {
            var that = this;
            var options = {};
            options.ID = item.ID;
            options.action = 'getcustomerservicestatus';
            ns.post(options, function (succeed, data, err) {
                if (data.status == true && data.ServiceStatus == item.ServiceStatus) {
                    ns.openWin('chooseserviceuser_frm', '派单处理', {
                        source: 'transfer',
                        id: item.ID,
                        type: type,
                        ServiceType: that.form.servicetype
                    });

                } else {
                    api.toast({
                        msg: '状态已改变，请重新选择',
                        duration: 2000,
                        location: 'bottom'
                    });
                    that.form.isFirstLoad = 1;
                    that.get_data();

                }


            });
        },
        //催单
        do_open_cuidan: function (id) {
            var that = this;
            var options = {};
            options.ID = id;
            options.action = "jpushuser";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    toast.custom({
                        title: "催单成功",
                        html: '<i class="icon iconfont icon-yidu"></i>',
                        duration: 2000
                    });

                    api.sendEvent({
                        name: 'reloadservicelist',
                    });
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true,
                toastmsg: '提交中'
            });
        },
        //拒绝
        do_open_nodialog: function (id) {
            var that = this;
            dialog.alert({
                title: "提示",
                msg: '拒绝接受该工单，工单将流转至派单人。是否确认拒绝？',
                buttons: ['取消', '确认拒绝']
            }, function (ret) {
                if (ret.buttonIndex == 2) {
                    var options = {};
                    options.ID = id;
                    options.action = "refuseservice";
                    ns.post(options, function (succeed, data, err) {
                        if (data.status == true) {
                            that.form.isFirstLoad = 1;
                            that.get_data();
                        } else {
                            api.toast({
                                msg: data.Msg,
                                duration: 2000,
                                location: 'bottom'
                            });
                            that.form.isFirstLoad = 1;
                            that.get_data();
                        }
                    }, {
                        toast: true,
                        toastmsg: '提交中'
                    });

                }
            })

        },
        //关单
        do_open_dialog: function (id) {
            var that = this;
            dialog.alert({
                title: "提示",
                msg: '工单关闭后，将不能进行任何操作，请确认工单是否已经处理完成？',
                buttons: ['取消', '确认关闭']
            }, function (ret) {
                if (ret.buttonIndex == 2) {
                    var options = {};
                    options.ID = id;
                    options.action = "serviceclose";
                    ns.post(options, function (succeed, data, err) {
                        if (succeed) {
                            that.form.isFirstLoad = 1;
                            that.get_data();
                            api.sendEvent({
                                name: 'reload_Analysis',
                            });
                        } else if (err) {
                            api.toast({
                                msg: err,
                                duration: 2000,
                                location: 'bottom'
                            });
                        }
                    }, {
                        toast: true,
                        toastmsg: '提交中'
                    });

                }
            })
        },
        //接单
        do_accpet: function (item) {
            var that = this;
            var options = {};
            options.ID = item.ID;
            options.action = "acceptservice";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    if (data.status == true) {
                        that.form.isFirstLoad = 1;
                        that.select_status(0);
                        // that.get_data();
                        api.sendEvent({
                            name: 'reloadservicelist',
                        });
                    } else {
                        api.toast({
                            msg: '状态已改变，请重新选择',
                            duration: 2000,
                            location: 'bottom'
                        });
                        that.form.isFirstLoad = 1;
                        that.get_data();
                    }
                }else if (err){
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }

            }, {
                toast: true,
                toastmsg: '提交中'
            });
        },
        //完工
        do_complete: function (item) {
            var that = this;
            var options = {};
            options.ID = item.ID;
            options.action = "getcustomerservicestatus";
            ns.post(options, function (succeed, data, err) {
                if (data.status == true && data.ServiceStatus == item.ServiceStatus) {
                    ns.openWin('renwugongdancomplete_frm', '完工记录', {
                        source: 'transfer',
                        id: item.ID,
                        type: 2,
                        ServiceType: item.ServiceType
                    });
                } else {
                    api.toast({
                        msg: '状态已改变，请重新选择',
                        duration: 2000,
                        location: 'bottom'
                    });
                    that.form.isFirstLoad = 1;
                    that.get_data();
                }
            }, {
                toast: true,
                toastmsg: '提交中'
            });

        },
        //处理
        do_chuli: function (item) {
            var that = this;
            ns.openWin('renwugongdancomplete_frm', '处理记录', {
                source: 'transfer',
                id: item.ID,
                type: 1,
                ServiceType: item.ServiceType
            });
        },
        //回访
        do_huifang: function (item) {
            var that = this;
            ns.openWin('renwugongdanreturnvisit', '回访记录', {
                source: 'transfer',
                id: item.ID,
                type: 3,
                ServiceType: item.ServiceType
            });
        },
        open_select:function(){
            var that=this;
            that.showTop=!that.showTop;
        },
        doHideSearch:function(){
            var that=this;
            that.showTop=false;
            that.showsearch=false;
        },
        select_allstatus:function(){
            var that=this;
            that.showsearch=!that.showsearch;
            
            var options={};
            options.servicetype = that.form.servicetype;//状态筛选，支持多选和单选
            options.action = "getservicehistory";
            ns.post(options, function(succeed, data, err) {
                if (succeed) {
                    that.searchlist = data.list;
                    
                } else if (err) {
                    that.list = [];
                    that.current_item_length = 0;
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });

        },
        //搜索结果返回位置
        do_scrollTo:function(){
            var that = this;
    　　　　 var sollid = document.getElementById('scroll'+that.form.ID);
    　　　　 window.scrollTo(0,sollid.offsetTop); 
            var classlist=document.getElementById('scroll'+that.form.ID).getElementsByClassName('boxbg')[0];
            classlist.style.backgroundColor = "#eaf1ff";
            setTimeout(function() {
                classlist.style.backgroundColor = "#f7fbff";
            }, 800)
            
    　　},


    }
});
apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    dialog = new auiDialog();
    toast = new auiToast();
    app.form.servicetype = api.pageParam.servicetype || 1;
    app.form.RoomID = api.pageParam.id || 0;
    app.form.isviewdetail = api.pageParam.isviewdetail || false;
    app.form.IsWorkOrder=api.pageParam.IsWorkOrder || 1;
    app.form.ID = api.pageParam.ID || 0;
    if (app.form.isviewdetail) {
        app.form.status = 4;
    }
    app.form.onlybaoshi = api.pageParam.onlybaoshi || 0;
    api.addEventListener({
        name: 'reloadservicelist'
    }, function (ret, err) {
        app.form.isFirstLoad = 1;
        app.get_data();
    });
    api.addEventListener({
        name: 'customerservicelist_frm'
    }, function (ret, err) {
        app.form.isFirstLoad = 1;
        app.get_data();
    });
    api.addEventListener({
        name: 'is_hidden_right',
    }, function (ret, err) {
        app.form.isFirstLoad = 1;
        app.get_data();
    });
    // ns.init_pull(true,true);
    ns.init_pull();
    app.get_data();
    setTimeout(function () {
        app.get_operation();
    }, 500)
    api.addEventListener({
        name: 'dosaveservicetype'
    }, function (ret) {
        app.servicetypename = ret.value.item.text;
        app.servicetypeid = ret.value.item.id;
        app.do_editType();

    });
    //清空
    api.addEventListener({
        name: 'do_open_delete'
    }, function (ret, err) {
        api.confirm({
            title: '提示信息',
            msg: '确认要清理所有的消息信息吗？',
            buttons: ['确定', '取消']
        }, function(ret, err) {
            if (ret) {
                if (ret.buttonIndex == 1) {
                    var options={};
                    options.action="doremoveallpushmsg";
                    options.Type=5;//1-安防巡更 2-环境保洁 3-设备巡检 4-设备维保 5-客服工单 6-投诉建议 7-审核任务
                    ns.post(options, function(succeed, data, err) {
                        if (succeed) {
                            app.get_data();
                        } else if (err) {
                            api.toast({
                                msg: err,
                                duration: 2000,
                                location: 'bottom'
                            });
                        }
                    }, {
                        toast: true,
                        toastmsg: '删除中'
                    });
                }
            }
        })
    });

    // api.addEventListener({
    //     name: 'do_open_select'
    // }, function(ret, err) {
    //     app.open_select();
    // });
};
